$(document).ready(function () {
    var data = <%= chart_data.inspect %>;
    var ticks = [<%= raw chart_ticks.map {|tick| tick.blank? ? "''" : "'#{tick.strftime("%d %b")}'"}.join(',') %>];
    var high_dat = [<%= raw highlighter_data.map {|dat| "['#{dat[0].strftime("%d %b")}', #{ (dat[1] * 100).round * 0.01}]"}.join(',') %>];

    var plot1 = $.jqplot(
            '<%= print ? 'tt_report-print-chart' : 'tt_reporting_chart' %>',
            [data],
            {
                <% if print %>
                seriesColors:['#999'],
                <% end %>
                seriesDefaults:{
                    renderer:$.jqplot.BarRenderer,
                    rendererOptions:{
                        fillToZero:true,
                        shadow:false
                    }
                },
                axes:{
                    xaxis:{
                        renderer:$.jqplot.CategoryAxisRenderer,
                        ticks:ticks
                    },
                    yaxis:{
                        <% if print %>
                        fontSize:'10px',
                        <% end %>
                        autoscale:true,
                        min:0,
                        pad:1,
                        tickInterval: 1,
                        tickOptions:{formatString:'%d<%= l(:tt_hours_sign) %>'}
                    }
                },
                grid:{
                    background:"#ffffff",
                    shadow:false
                },
                highlighter:{
                    tooltipContentEditor:function (str, seriesIndex, pointIndex, plot) {
                        var point = high_dat[pointIndex];
                        return point[0] + this.tooltipSeparator + time_dist2string(point[1] * 3600);
                    },
                    show:true,
                    showMarker:false
                }
            });
            $(window).resize(function() {
                    plot1.replot( {resetAxes: true } );
                });
});


function time_dist2string(dist) {
    var h = Math.floor(dist / 3600);
    var m = Math.floor((dist - h * 3600) / 60);
    return h.toString() + "<%= l(:tt_hours_sign) %> " + m.toString() + "<%= l(:tt_min_sign) %>";
}